//
//  CommunityViewController.m
//  Qyer
//
//  Created by dllo on 15/10/19.
//  Copyright (c) 2015年 GF. All rights reserved.
//

#import "CommunityViewController.h"
#import "PromptModel.h"
@interface CommunityViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UISearchBarDelegate, UITableViewDataSource, UITableViewDelegate, UIScrollViewDelegate>
@property (nonatomic, strong)CommunityModel *comModel;
@property (nonatomic, strong)NSMutableArray *coreDataArr;
@property (nonatomic, strong)NSArray *headArr;
@property (nonatomic, strong)NSMutableArray *forumArr;
@property (nonatomic, strong)CoreDataManager *coreDataManager;
@property (nonatomic, strong)UISearchBar *searchBar;
@property (nonatomic, strong)UIView *coverView;
@property (nonatomic, strong)UIScrollView *scrollView;
@property (nonatomic, strong)UIView *lineView;

@property (nonatomic, assign)NSInteger type; // 判断网络请求类型
@property (nonatomic, strong)NSMutableArray *hotDoorArr;
@property (nonatomic, strong)NSMutableArray *promptArr; // 搜索提示数组
@property (nonatomic, strong)NSMutableArray *listArr;
@property (nonatomic, assign)BOOL isLoad;
@property (nonatomic, assign)CGFloat height;
@property (nonatomic, assign)CGFloat hotWidth;
@property (nonatomic, assign)NSInteger page;
@end

@implementation CommunityViewController
- (void)viewWillAppear:(BOOL)animated {
    _searchBar.hidden = NO;
    _searchBar.delegate = self;
    self.navigationController.navigationBar.hidden = NO;
    self.tabBarController.tabBar.hidden = NO;
}
- (void)viewWillDisappear:(BOOL)animated {
    [GiFHUD dismiss];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.coreDataManager = [CoreDataManager shareManager];
    self.automaticallyAdjustsScrollViewInsets = NO;
    //NSLog(@"%@", self.coreDataManager.applicationDocumentsDirectory);
    _coreDataArr = [NSMutableArray arrayWithArray:[self.coreDataManager searchCoreData:@"date"]];
    _forumArr = [[NSMutableArray alloc] init];
    [_coreDataManager saveContext];
    [self creatForumSearchBar];
    [self getData];
    if (_scrollView == nil) {
        [self creatSearchTableView];
    }
    [self addFooter];
    _searchTableView.footer.hidden = YES;
    [GiFHUD setGifWithImageName:@"9707070_11.gif"];
    [GiFHUD show];
}
#pragma mark - 创建searchBar
- (void)creatForumSearchBar {
    _searchBar = [[UISearchBar alloc] initWithFrame:CRM(20 * WIDTH, 5, SCREEN_WIDTH - 40 * WIDTH, 30)];
    _searchBar.placeholder = @"搜索帖子";
    self.type = 0;
    _searchBar.barTintColor = UIColorWithRGBA(16, 168, 128, 1.0);
    _searchBar.showsCancelButton = NO;
    //_searchBar.searchBarStyle = UISearchBarStyleProminent;
    //_searchBar.showsSearchResultsButton = YES;
    _searchBar.barTintColor = [UIColor redColor];
    UITextField *textField = [((UIView *)[_searchBar.subviews objectAtIndex:0]).subviews lastObject];
    textField.backgroundColor = UIColorWithRGBA(16, 168, 128, 1.0);
    textField.layer.cornerRadius = 13;
     [textField setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 16, 16)];
    imgView.image = [UIImage imageNamed:@"iconfont-sousuo"];
    textField.leftView = imgView;
    
    _searchBar.delegate = self;
    [self.navigationController.navigationBar addSubview:_searchBar];
}
- (void)creatSearchTableView {
    _promptArr = [[NSMutableArray alloc] init];
    _listArr = [[NSMutableArray alloc] init];
    _hotDoorArr = [[NSMutableArray alloc] init];
    
    _coverView = [[UIView alloc] initWithFrame:CRM(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64)];
    _coverView.backgroundColor = [UIColor clearColor];
    _coverView.hidden = YES;
    _scrollView = [[UIScrollView alloc] initWithFrame:CRM(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64)];
    _scrollView.contentSize = CSM(0, _scrollView.frame.size.height * 2);
    _scrollView.backgroundColor = [UIColor redColor];
    _scrollView.bounces = NO;
    _scrollView.scrollEnabled = NO;
    //_scrollView.userInteractionEnabled = NO;
    _scrollView.pagingEnabled = YES;
    _scrollView.delegate = self;
    
    //_scrollView.hidden = YES;
    _scrollView.backgroundColor = [UIColor clearColor];
    [_coverView addSubview:_scrollView];
    [self.view addSubview:_coverView];
    _searchTableView = [[UITableView alloc] initWithFrame:CRM(0, SCREEN_HEIGHT - 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:UITableViewStylePlain];
    _searchTableView.backgroundColor = DAY_BACKGROUND;
    _searchTableView.dataSource = self;
    _searchTableView.delegate = self;
    //_searchTableView.userInteractionEnabled = YES;
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor clearColor];
    _searchTableView.tableFooterView = view;
    [_scrollView addSubview:_searchTableView];
    
    _type = 0;
    [self getDataWithText:@""];
}

#pragma mark 点击输入框
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
    _searchBar.showsCancelButton = YES;
    self.tabBarController.tabBar.hidden = YES;
    NSLog(@"即将编辑");
#pragma mark 取消按钮样式
{
    // 查看searchBar的子控件
    //NSLog(@"%@", [_searchBar performSelector:@selector(recursiveDescription)]);
    //iOS7+ : 改变取消按钮
    for(id cancel in [_searchBar.subviews[0] subviews]){
        if ([cancel isKindOfClass:[UIButton class]]) {
            UIButton *btn = (UIButton *)cancel;
            [btn setTitle:@"取消" forState:UIControlStateNormal];
            [btn setTitleColor:WHITE_COLOR forState:UIControlStateNormal];
        }
    }
//    for (id obj in [_searchBar subviews]) {
//        if ([obj isKindOfClass:[UIView class]]) {
//            for (id obj2 in [obj subviews]) {
//                if ([obj2 isKindOfClass:[UIButton class]]) {
//                    UIButton *btn = (UIButton *)obj2;
//                    [btn setTitle:@"取消" forState:UIControlStateNormal];
//                }
//            }
//        }
//    }
    
//      iOS7- :     for(id cc in [searchBar subviews]){}
    }
    //_scrollView.hidden = NO;
    _coverView.hidden = NO;
    [_scrollView setContentOffset:CGPointMake(0, SCREEN_HEIGHT - 64) animated:YES];
    _searchTableView.footer.hidden = YES;
    return YES;
}
#pragma mark 即将输入
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
    NSLog(@"即将输入");
    searchBar.text = @"";
    self.type = 0;
    NSLog(@"arrcount == %ld", _hotDoorArr.count);
    [_searchTableView reloadData];
    [_promptArr removeAllObjects];
    [_listArr removeAllObjects];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView == _searchTableView) {
        if (self.type == 2) {
            [_searchBar resignFirstResponder];
        }
    }
}
#pragma mark 输入信息改变时
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
    // 搜索提示 自定义type = 1;
    NSLog(@"信息改变");
    [_promptArr removeAllObjects];
    self.type = 1;
    [self getDataWithText:searchText];
    _searchTableView.footer.hidden = YES;
}
#pragma mark 点击搜索
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    self.type = 2;
    self.page = 1;
    _isLoad = NO;
    [self getDataWithText:searchBar.text];
}

#pragma mark 取消
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
    _searchBar.showsCancelButton = NO;
    self.tabBarController.tabBar.hidden = NO;
    [_searchBar resignFirstResponder];
    //_scrollView.hidden = YES;
    [_scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
    _coverView.hidden = YES;
    _searchTableView.footer.hidden = YES;
}
#pragma mark - searchBar网络请求
- (void)getDataWithText:(NSString *)text {
    if ([Reachability isConnectedInternet]) {
        if (self.type == 1) {
            [AFN getdataWithString:[NSString stringWithFormat:@"http://open.qyer.com/qyer/search/autocomplate?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&keyword=%@&lat=38.88261581406422&lon=121.5396390461619&page=1", text] block:^(id block) {
                [_promptArr removeAllObjects];
                NSDictionary *dic = [block objectForKey:@"data"];
                if ([dic count] != 0) {
                    for (NSString *str in [dic objectForKey:@"entry"]) {
                        [_promptArr addObject:str];
                    }
                }
                [_searchTableView reloadData];
            } BLOCK:^(id BLOCK) {
            }];
        }
        else if (self.type == 2) {
            [GiFHUD show];
            [AFN getdataWithString:[NSString stringWithFormat:@"http://open.qyer.com/qyer/search/index?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&keyword=%@&lat=38.88261581406422&lon=121.5396390461619&page=%ld&type=thread&v=1", text, self.page] block:^(id block) {
                NSDictionary *dic = [NSDictionary dictionaryWithDictionary:[block objectForKey:@"data"]];
                if ([dic count] != 0) {
                    if (_isLoad == NO) {
                        self.page = 1;
                        [_listArr removeAllObjects];
                    }
                    for (NSDictionary *dictionay in [dic objectForKey:@"entry"]) {
                        PostModel *model = [PostModel modelWithDictionary:dictionay];
                        [_listArr addObject:model];
                    }
                    [_searchTableView.legendFooter endRefreshing];
                    [_searchTableView reloadData];
                    _searchTableView.footer.hidden = NO;
                    
                } else {
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"未找到" message:@"请输入正确搜索信息" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                    [alert show];
                }
                [GiFHUD dismiss];
            } BLOCK:^(id BLOCK) {
            }];
        } else {
            [AFN getdataWithString:@"http://open.qyer.com/qyer/search/hot_history?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&lat=38.88261581406422&lon=121.5396390461619&page=1&track_app_channel=App%2520Store&track_app_version=6.7&track_device_info=iPad%2520mini%2520%28Wifi%29&track_deviceid=D44778F3-A8BB-4217-B3AD-DB6BF0A1D8B2&track_os=ios%25208.4.1&v=1" block:^(id block) {
                for (NSString *str in [block objectForKey:@"data"]) {
                    [_hotDoorArr addObject:str];
                }
                [_searchTableView reloadData];
            } BLOCK:^(id BLOCK) {
                
            }];
        }
    }
}
#pragma mark 上拉加载
- (void)addFooter {
    WS(weakSelf);
    [_searchTableView addLegendFooterWithRefreshingBlock:^{
        weakSelf.isLoad = YES;
        weakSelf.page = weakSelf.page + 1;
        NSLog(@"weak.page === %ld", weakSelf.page);
        NSLog(@"weak.text == =%@", weakSelf.searchBar.text);
        
        [weakSelf getDataWithText:[NSString stringWithFormat:@"http://open.qyer.com/qyer/search/index?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&keyword=%@&lat=38.88261581406422&lon=121.5396390461619&page=%ld&type=thread&v=1", weakSelf.searchBar.text, weakSelf.page]];
    }];
}
#pragma mark - 创建桌面
- (void)creatCollectionView {
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.minimumLineSpacing = 1.0;
    flowLayout.minimumInteritemSpacing = 1.0;
    
    flowLayout.footerReferenceSize = CSM(SCREEN_WIDTH, 20 * HEIGHT);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    _collectionView = [[UICollectionView alloc] initWithFrame:CRM(0, NAVIGATION_BAR_HEIGHT, SCREEN_WIDTH, TABLE_HEIGHT) collectionViewLayout:flowLayout];
    _collectionView.backgroundColor = DAY_BACKGROUND;
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    [self.view insertSubview:_collectionView atIndex:0];
    // 注册cell
    [_collectionView registerClass:[CommunityHeadCell class] forCellWithReuseIdentifier:@"headCell"];
    [_collectionView registerClass:[CommunityCell class] forCellWithReuseIdentifier:@"cell"];
    // 注册header
    [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header1"];
    [_collectionView registerClass:[HeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    // 注册footer
    [_collectionView registerClass:[FooterView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer"];
}
#pragma mark - 网络请求
- (void)getData {
    if ([Reachability isConnectedInternet]) {
        [AFN getdataWithString:@"http://open.qyer.com/qyer/bbs/entry?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&lat=38.88259977358427&lon=121.5397371891666&page=1&track_app_channel=App%2520Store&track_app_version=6.7&track_device_info=iPad%2520mini%2520%28Wifi%29&track_deviceid=D44778F3-A8BB-4217-B3AD-DB6BF0A1D8B2&track_os=ios%25208.4.1&v=1" block:^(id block) {
            for (NSDictionary *dic in [[block objectForKey:@"data"] objectForKey:@"forum_list"]) {
                [_forumArr addObject:dic];
            }
            NSString *askStr = [NSString stringWithFormat:@"%@个问题得到解决", [[[block objectForKey:@"data"] objectForKey:@"counts"] objectForKey:@"ask"]];
            NSString *companyStr = [NSString stringWithFormat:@"%@个网友在此结伴", [[[block objectForKey:@"data"] objectForKey:@"counts"] objectForKey:@"company"]];
            
            _headArr = @[@"问答", @"结伴", @"iconfont-wenda", @"iconfont-users", askStr, companyStr];
            if (_collectionView == nil) {
                [self creatCollectionView];
            } else {
                [_collectionView reloadData];
            }
            [GiFHUD dismiss];
        } BLOCK:^(id BLOCK) {
            
        }];
    }
}
#pragma mark - collectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    if (collectionView != _collectionView) {
        return 1;
    } else {
        
        return 2 + _forumArr.count;
    }
    
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (collectionView != _collectionView) {
            return _hotDoorArr.count;
    } else {
        if (section == 0) {
            return 2;
        }else if (section == 1) {
            return _coreDataArr.count;
        } else {
            NSDictionary *dic = [_forumArr objectAtIndex:section - 2];
            NSArray *arr = [dic objectForKey:@"group"];
            return arr.count;
        }
    }
    
}
#pragma mark header & footer Size
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    if (collectionView != _collectionView) {
        return CSM(0, 0);
    } else {
        if (section == 0) {
            return CSM(SCREEN_WIDTH, 20 * HEIGHT);
        } else {
            return CSM(SCREEN_WIDTH, 40 * HEIGHT);
        }
    }
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if (collectionView != _collectionView) {
        return nil;
    } else {
        if ([kind isEqual:UICollectionElementKindSectionHeader]) {
            // 从重用池中获取header
            UICollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header1" forIndexPath:indexPath];
            HeaderView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
            if (indexPath.section == 0) {
                header.backgroundColor = WHITE_COLOR;
                return header;
            }
            else if (indexPath.section == 1) {
                headerView.titleLabel.text = @"最近浏览";
                return headerView;
            }
            else {
                headerView.titleLabel.text = [[_forumArr objectAtIndex:indexPath.section - 2] objectForKey:@"name"];
                return headerView;
            }
        } else {
            FooterView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"footer" forIndexPath:indexPath];
            if (indexPath.section == 0) {
                footerView.backgroundColor = WHITE_COLOR;
            } else {
                footerView.backgroundColor = _collectionView.backgroundColor;
            }
            return footerView;
        }
        
    }
    
}
#pragma mark 创建item
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (collectionView != _collectionView) {
        HostDoorCCell *hotCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collection" forIndexPath:indexPath];
        hotCell.label.text = [_hotDoorArr objectAtIndex:indexPath.item];
        hotCell.label.frame = hotCell.bounds;
        return hotCell;
    } else {
        CommunityHeadCell *headCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"headCell" forIndexPath:indexPath];
        CommunityCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
        if (indexPath.section == 0) {
            [headCell setCellWithArr:_headArr index:indexPath.item];
            return headCell;
        }
        else if (indexPath.section == 1) {
            Forum *forumModel = [_coreDataArr objectAtIndex:indexPath.row];
            [cell setCellWithForum:forumModel];
            return cell;
        }
        else {
            CommunityModel *model = [CommunityModel modelWithDictionary:[[[_forumArr objectAtIndex:indexPath.section - 2] objectForKey:@"group"] objectAtIndex:indexPath.row]];
            [cell setCellWithModel:model];
            return cell;
        }
    }
}
#pragma mark - collectionViewDelegate & flowLayoutDelegate
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (collectionView != _collectionView) {
        CGFloat width = [StringWithWidth widthWithText:[_hotDoorArr objectAtIndex:indexPath.item] height:40 font:[UIFont systemFontOfSize:16]];
        UILabel *label = [[UILabel alloc] initWithFrame:CRM(0, 0, width, 40 * HEIGHT)];
        label.text = [_hotDoorArr objectAtIndex:indexPath.item];
        label.font = [UIFont systemFontOfSize:16];
        self.hotWidth = label.frame.size.width + 40 * WIDTH;
        return CSM(self.hotWidth, 40 * HEIGHT);
    } else {
        if (indexPath.section == 0) {
            CGSize size = CSM(SCREEN_WIDTH / 2 - 1, 90 * HEIGHT);
            return size;
        } else {
            CGSize size = CSM((SCREEN_WIDTH - 45 * WIDTH) / 2, 100 * HEIGHT);
            return size;
        }
    }
}
//- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
//{
//    if (section == 0) {
//        return 1;
//    }else{
//        return 20;
//    }
//}
#pragma mark 内置空间
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    if (collectionView != _collectionView) {
        return UIEdgeInsetsMake(20 * WIDTH, 20 * WIDTH, 20 * WIDTH, 20 * WIDTH);
    } else {
        if (section == 0) {
            UIEdgeInsets inset = UIEdgeInsetsMake(0, 0, 0, 0);
            return inset;
        }else{
            UIEdgeInsets inset = UIEdgeInsetsMake(10 * WIDTH, 20 * WIDTH, 0, 20 * WIDTH);
            return inset;
        }
    }
    
}
#pragma mark - 点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    if (collectionView != _collectionView) {
        self.type = 2;
        _isLoad = NO;
        _searchBar.text = [_hotDoorArr objectAtIndex:indexPath.item];
        [self getDataWithText:_searchBar.text];
    } else {
        if (indexPath.section != 0) {
            Forum *forumInfo = [NSEntityDescription insertNewObjectForEntityForName:@"Forum" inManagedObjectContext:self.coreDataManager.managedObjectContext];
            NSDictionary *dic = [NSDictionary dictionary];
            if (indexPath.section != 1) {
                dic = [[[_forumArr objectAtIndex:indexPath.section - 2] objectForKey:@"group"] objectAtIndex:indexPath.row];
            } else {
                Forum *forum = [_coreDataArr objectAtIndex:indexPath.row];
                NSArray *array = [NSKeyedUnarchiver unarchiveObjectWithData:forum.types];
                dic = [NSDictionary dictionaryWithObjects:@[forum.cId, forum.name, forum.photo, forum.total_threads, array] forKeys:@[@"id", @"name", @"photo", @"total_threads", @"types"]];
            }
            forumInfo.name = [dic objectForKey:@"name"];
            forumInfo.cId = [dic objectForKey:@"id"];
            forumInfo.date = [NSDate date];
            forumInfo.total_threads = @([[dic objectForKey:@"total_threads"] integerValue]);
            forumInfo.photo = [dic objectForKey:@"photo"];
            NSArray *typesArr = [dic objectForKey:@"types"];
            // 数组转NSData
            forumInfo.types = [NSKeyedArchiver archivedDataWithRootObject:typesArr];
            //保存
            [self.coreDataManager saveContext];
            
            int count = 0;
            for (Forum *forum in _coreDataArr) {
                if ([forum.name isEqualToString:forumInfo.name]) {
                    count ++;
                    if (count == 1) {
                        [self.coreDataManager.managedObjectContext deleteObject:forum];
                        [_coreDataArr removeObject:forum];
                        [self.coreDataManager saveContext];
                        break;
                    }
                }
            }
            
            if (_coreDataArr.count == 6) {
                [self.coreDataManager.managedObjectContext deleteObject:_coreDataArr.lastObject];
                [_coreDataArr removeLastObject];
                [_coreDataManager saveContext];
            }
            _coreDataArr = [NSMutableArray arrayWithArray:[self.coreDataManager searchCoreData:@"date"]];
            NSLog(@"存储数量 == %ld", _coreDataArr.count);
            [collectionView reloadData];
            DetailViewController *detail = [[DetailViewController alloc] init];
            detail.dic = dic;
            [self.navigationController pushViewController:detail animated:YES];
            _searchBar.hidden = YES;
        } else {
            if (indexPath.row == 0) {
                AskViewController *ask = [[AskViewController alloc] init];
                [self.navigationController pushViewController:ask animated:YES];
            } else {
                CommunityWeb *web = [[CommunityWeb alloc] init];
                web.urlStr = @"http://www.biqugex.com/book_813/";
                [self.navigationController pushViewController:web animated:YES];
            }
        }
    }
    
    
}

#pragma mark - 搜索界面代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.type == 1) {
        return _promptArr.count;
    } else if (self.type == 2) {
        return _listArr.count;
    } else {
        return 1;
    }
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *indentifier = @"prompt";
    UITableViewCell *promptCell = [tableView dequeueReusableCellWithIdentifier:indentifier];
    if (!promptCell) {
        promptCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentifier];
    }
    static NSString *cellId = @"mainCell";
    CellForPostModel *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell) {
        cell = [[CellForPostModel alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    }
    static NSString *hotId = @"hot";
    HotDoorTCell *hotCell = [tableView dequeueReusableCellWithIdentifier:hotId];
    if (!hotCell) {
        hotCell = [[HotDoorTCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:hotId];
    }
    hotCell.delegate = self;
    hotCell.dataSource = self;
    if (self.type == 1) {
//        NSDictionary *dic =[_promptArr objectAtIndex:indexPath.row];
//        NSLog(@"%@",dic);
//        //NSLog(@"%@", str);
        promptCell.textLabel.text = [_promptArr objectAtIndex:indexPath.row];
        self.height = 50 * HEIGHT;
        return promptCell;
    } else if (self.type == 2) {
        PostModel *model = [_listArr objectAtIndex:indexPath.row];
        [cell setData:model text:_searchBar.text];
        self.height = cell.height + 90 * HEIGHT;
        return cell;
    } else {
        //hotCell.dataArr = _hotDoorArr;
        [hotCell.collectionView reloadData];
        return hotCell;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.type == 0) {
        return 300 * HEIGHT;
    } else {
        return self.height;
    }
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (self.type == 1) {
        self.type = 2;
        _searchBar.text = [_promptArr objectAtIndex:indexPath.row];
        [self getDataWithText:[_promptArr objectAtIndex:indexPath.row]];
    } else if (self.type == 2) {
        CommunityWeb *web = [[CommunityWeb alloc] init];
        PostModel *model = [_listArr objectAtIndex:indexPath.row];
        web.urlStr = model.view_url;
        [self.navigationController pushViewController:web animated:YES];
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
